package com.fortune.service.impl;

import com.fortune.constants.enums.ProductStock;
import com.fortune.dao.MProductStockDetailInfoDao;
import com.fortune.dao.MStoreInDao;
import com.fortune.dto.in.mStoreIn.MStoreInAddDto;
import com.fortune.dto.in.mStoreIn.MStoreInEditDto;
import com.fortune.dto.in.mStoreIn.MStoreInListDto;
import com.fortune.service.MStoreInService;
import com.fortune.vo.mStoreIn.MStoreInInfo;
import com.fortune.vo.mStoreIn.MStoreInInfoDetail;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class MStoreInServiceImpl implements MStoreInService {
    @Autowired
    private MStoreInDao mStoreInDao;
    @Autowired
    private MProductStockDetailInfoDao mProductStockDetailInfoDao;

    /**
     * 入库单列表
     * @param companyId
     * @param mStoreInListDto
     * @return
     */
    @Override
    public List<MStoreInInfo> queryMStoreInList(String companyId,MStoreInListDto mStoreInListDto) {
        return mStoreInDao.queryMStoreInList(companyId,mStoreInListDto);
    }

    /**
     * 入库单列表个数
     * @param companyId
     * @param mStoreInListDto
     * @return
     */
    @Override
    public int queryMStoreInListCount(String companyId, MStoreInListDto mStoreInListDto) {
        return mStoreInDao.queryMStoreInListCount(companyId,mStoreInListDto);
    }

    /**
     * 入库单详情
     * @param companyId
     * @param id
     * @return
     */
    @Override
    public MStoreInInfoDetail queryMStoreInInfoDetail(String companyId, String id) {
        return mStoreInDao.queryMStoreInInfoDetail(companyId,id);
    }

    /**
     * 新增入库单
     * @param userId
     * @param companyId
     * @param addDto
     * @return
     */
    @Override
    @Transactional
    public int addMStoreIn(String userId, String companyId, MStoreInAddDto addDto) {
        //入库单ID
        String id = UUID.randomUUID().toString().replace("-","");
        int num = mStoreInDao.addMStoreIn(userId,id,companyId,addDto);
        if (num < 1){
            throw new RuntimeException("新增失败");
        }
        num = mStoreInDao.addMStoreInInfo(companyId,id,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("新增失败");
        }
        return 1;
    }

    /**
     * 编辑入库单
     * @param companyId
     * @param editDto
     * @return
     */
    @Override
    @Transactional
    public int editMStoreIn(String companyId, MStoreInEditDto editDto) {
        int num = mStoreInDao.editMStoreIn(companyId,editDto);
        if (num < 1){
            throw new RuntimeException("编辑入库单失败");
        }
        num = mStoreInDao.deleteMStoreInInfo(companyId,editDto.getId());
        if (num < 1){
            throw new RuntimeException("编辑入库单失败");
        }
        num = mStoreInDao.editMStoreInInfo(companyId,editDto.getId(),editDto.getProduce());
        if (num < 1){
            throw new RuntimeException("编辑入库单失败");
        }
        return 1;
    }

    /**
     * 删除入库单
     * @param companyId
     * @param id
     * @return
     */
    @Override
    public int deleteMStoreIn(String companyId, String id) {
        return mStoreInDao.deleteMStoreIn(companyId,id);
    }

    /**
     * 验收入库
     * @param companyId
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int updateMStoreInType(String companyId, String id) {
        int num = mStoreInDao.updateMStoreInType(companyId,id);
        if (num < 1){
            throw new RuntimeException("验收入库失败");
        }
        List<ProductStockDetailInfo> list = mStoreInDao.queryProductIn(companyId,id);
//        Date date = new Date();
//        num = mProductStockDetailInfoDao.insertProductStockDetail(companyId,date, ProductStock.SCRK,list);
//        if (num < 1){
//            throw new RuntimeException("验收入库失败");
//        }
        for (int i = 0; i < list.size(); i++){
            ProductStockDetailInfo detailInfo = list.get(i);
            int order = mProductStockDetailInfoDao.queryMax(companyId,detailInfo.getStoreId(),detailInfo.getMpid());
            List<ProductStockDetailInfo> lastDate = mProductStockDetailInfoDao.queryLastStockDetail(companyId,detailInfo.getStoreId(),detailInfo.getMpid());
            if (lastDate != null){
                detailInfo.setRemainNum(lastDate.get(0).getRemainNum()+detailInfo.getInNum());
            }
            num = mProductStockDetailInfoDao.insertSingleInProductStockDetail(companyId,ProductStock.SCRK,detailInfo,order);
            if (num < 1){
                throw new RuntimeException("验收入库失败");
            }
        }
        return 1;
    }
}
